package ahblue.cn.tanzi.ui.person;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.niuedu.ListTree;

import java.util.List;

import javax.security.auth.login.LoginException;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.adapter.MyTeameAdapter;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.jmessage.im.pickerimage.utils.StringUtil;
import ahblue.cn.tanzi.model.circle.CircleDetailTop;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.model.user.TeamModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyTeamActivity extends BaseActivity {

    @BindView(R.id.ll_left)
    LinearLayout llLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.listview)
    RecyclerView listView;


    private String currentUserName;
    //保存数据的集合
    private ListTree tree=new ListTree();
    //从ListTreeAdapter派生的Adapter
    MyTeameAdapter adapter;
    ListTree.TreeNode currentUser;
    RemoteRepository repo;
    String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_team);
        ButterKnife.bind(this);
        initDate();

    }

    private void initDate(){
        tvTitle.setText("我的团队");

        token = LoginResultModel.getToken(this);
        repo = RemoteRepository.getsInstance();
        repo.getMyTeam(token, mMyTeamCallback);

        //创建后台数据：一棵树
        //创建组们，是root node，所有parent为null
        MyTeameAdapter.ContactInfo parentInfo = new MyTeameAdapter.ContactInfo(null,"我","");
        currentUser=tree.addNode(null,parentInfo, R.layout.contacts_group_item);

    }

    @OnClick(R.id.btn_back)
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
        }
    }

    private void getTeam(ListTree.TreeNode node,String code){
        currentUser = node;
        repo.getRelation(token,code,mRelationCallback);
    }


    private ApiCallback<List<TeamModel>> mMyTeamCallback = new ApiCallback<List<TeamModel>>() {


        @Override
        public void onSuccess(List<TeamModel> data) {

            Log.e("kzg","******************************data:"+data);
            //第二层
            if (data != null && data.size() > 0){
                for (TeamModel teamModel : data){
                    String name = teamModel.getMember_name();
                    if (StringUtil.isEmpty(name)){
                        name = "未命名";
                    }
                    MyTeameAdapter.ContactInfo info = new MyTeameAdapter.ContactInfo(null,name,teamModel.getInvite_code());
                    ListTree.TreeNode contactNode1=tree.addNode(currentUser,info,R.layout.contacts_group_item);
                    if ("0".equals(teamModel.getHas_more())){
                        contactNode1.setShowExpandIcon(false);
                    }else {
                        contactNode1.setShowExpandIcon(true);
                    }

                }

                adapter=new MyTeameAdapter(tree);
                adapter.setOnNodeClickListener(new MyTeameAdapter.OnNodeClickListener() {
                    @Override
                    public void onNodeClick(ListTree.TreeNode node, String code) {
                        if (StringUtil.isEmpty(code)){
                            return;
                        }
                        getTeam(node,code);
                    }
                });
            }else {
                adapter=new MyTeameAdapter(tree);
                currentUser.setShowExpandIcon(false);
            }


            listView.setLayoutManager(new LinearLayoutManager(MyTeamActivity.this));
            listView.setAdapter(adapter);

        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };

    private ApiCallback<List<TeamModel>> mRelationCallback = new ApiCallback<List<TeamModel>>() {


        @Override
        public void onSuccess(List<TeamModel> data) {

            //第二层
            if (data != null && data.size() > 0){
                for (TeamModel teamModel : data){
                    String name = teamModel.getMember_name();
                    if (StringUtil.isEmpty(name)){
                        name = "未命名";
                    }
                    MyTeameAdapter.ContactInfo info = new MyTeameAdapter.ContactInfo(null,name,teamModel.getInvite_code());
                    ListTree.TreeNode contactNode1=tree.addNode(currentUser,info,R.layout.contacts_group_item);
                    if ("0".equals(teamModel.getHas_more())){
                        contactNode1.setShowExpandIcon(false);
                    }else {
                        contactNode1.setShowExpandIcon(true);
                    }
                    adapter.notifyTreeItemInserted(currentUser,contactNode1);
                }
            }

            adapter.setOnNodeClickListener(new MyTeameAdapter.OnNodeClickListener() {
                @Override
                public void onNodeClick(ListTree.TreeNode node, String code) {
                    Log.e("kzg","**********************:"+code);
                    getTeam(node,code);
                }
            });

        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };
}
